package ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fy.fyzf.R;
import com.fy.fyzf.api.ApiRetrofit;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BasePresenter;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.dialog.DeleteWarningDialog;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.CacheUtil;
import com.fy.fyzf.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SettingActivity extends BaseActivity {


    @BindView(R.id.clear_cache)
    RelativeLayout clearCache;
    @BindView(R.id.tv_logout)
    TextView tvLogout;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("设置");
    }

    @Override
    public void initData() {
        super.initData();

    }


    @OnClick({R.id.clear_cache, R.id.tv_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear_cache:
                CacheUtil.clearAllCache(this);
                break;
            case R.id.tv_logout:
                DeleteWarningDialog deleteWarningDialog = new DeleteWarningDialog(this);
                deleteWarningDialog.setTitle("温馨提示");
                deleteWarningDialog.setContent("是否退出登录？");
                deleteWarningDialog.toggleDialog();
                deleteWarningDialog.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loginOut();
                    }
                });
                break;
        }
    }

    private void loginOut() {
        ApiRetrofit.getInstance().getApiService().loginOut(null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseData baseData) {
                        if (baseData.code.equals("0")) {
                            SPUtils.getInstance(SettingActivity.this).clear();
                            startActivity(new Intent(SettingActivity.this,LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                        } else {

                        }
                    }
                });
    }
}